﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;



namespace logwiz
{
    

    public partial class Form1 : Form
    {
        string sCompName; // to store the name of the local computer 
        string sTrgtCompName; //to run the perfmon log on. Incase the user is trying to run the logs remotely. Need to compare the name later.
        int sz; // total number of perf counters on the server 
        string[] chkboxname = new string[500];
        string cmd1, cmd2, cmd3, cmd5; // variable to set the logman command
        //cmd3 is for advanced. cmd5 is for general
        string cmd4;  // the string to start the perfmon that we just created.
        Process myProcess = new Process(); // instance of the process object
        string cd;
        static string tn; // for storing the name of the data collector set

        //public string[] RemoveDuplicates(string[] myList)
        //{
        //    System.Collections.ArrayList newList = new System.Collections.ArrayList();
        //    foreach (string str in myList)
        //        if (!newList.Contains(str))
        //            newList.Add(str);
        //    return (string[])newList.ToArray(typeof(string));
        //}


         public void makename()
        {
           // cd = DateTime.Now.ToShortTimeString();
            cd = DateTime.Now.ToLongTimeString();
            cd = cd.Replace(" ", "_");
            cd = cd.Replace("/", "_");
            cd = cd.Replace("\\", "_");
            cd = cd.Replace("-", "_");
            cd = cd.Replace(":", "_");
            int s = cd.Length;
            if (s>8)//change the size of the string if its more than 7. if less then leave it as it is.
            cd = cd.Remove(7); 

            tn = "logwiz" + "-" + cd;
        }

         public void showcmd() // for showing the command in the text box
         {
             if (radioButton1.Checked == true)
                 textBox8.Text = "logman.exe " + cmd5;  // for the generall method
             else
                 textBox8.Text = "logman.exe " + cmd3; // for the advanced method

             textBox8.Text = textBox8.Text + "\r\n" + cmd4; // putting everything in the textbox
         }

        public void addcounters()  // for adding the counters dynamically to the form once we load it.

        {
            int topPosition = 10;
            PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories();
            sz = perfCounters.Length;
            string[] perfcountername = new string[sz];
            string[] chkboxname = new string[sz];
            string[] unique = new string[sz];
            for (int i = 0; i < perfCounters.Length; i++)
            {
                perfcountername[i] = perfCounters[i].CategoryName.ToString();
            }
            int j = 0;
            Array.Sort(perfcountername);  // to sort the counters in  accending order
            
           foreach (string pc in perfcountername)
            {
                CheckBox checkBox = new CheckBox();
                checkBox.Top = topPosition;
                checkBox.Left = 10;
                checkBox.Text = pc;
                checkBox.Width = 170;
                //checkBox.AutoSize = true;

                topPosition += 10;
                checkBox.Name = chkboxname[j];
                flowLayoutPanel1.Controls.Add(checkBox);
                j++;
            }
        }//end of add counters

       
        

        public void createcmd() // for creating the logman command in advanced mode
        {
            makename();
            listBox1.Items.Clear();
            cmd1 = cmd2 = null;
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (c is CheckBox)
                {
                    CheckState x = ((CheckBox)c).CheckState; // Do whatever you want to do with CheckedState ( Value Of Check box )
                    if (((CheckBox)c).Checked == true)
                    {
                        listBox1.Items.Add(c.Text);
                        cmd1 = "\"\\"+ c.Text +"(*)\\*\" ";
                        cmd2 = cmd2 + cmd1;
                                   
                    }
                }

            }

           if ((listBox1.Items.Count.Equals(0)) && ((radioButton2.Checked == true)))
            {
                MessageBox.Show("You have not selected any objects to add. Please go back and select some objects to monitor", "No objects selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //button7.Enabled = true;
             }

            // for unique names 
           cmd3 = "create counter " + tn + " -o \""+ textBox5.Text +"\\logwiz\" -c " + cmd2 + " -f bincirc -max " + textBox4.Text + " -si "  + textBox7.Text + " -v mmddhhmm -ow";
           cmd4 = "start " + tn;   

       }

        public void starttheprocess()
        {
             //Turn on the hourglass cursor here.
                try
                {
                    this.Cursor = Cursors.WaitCursor;
                    myProcess.StartInfo.ErrorDialog = true;
                    myProcess.StartInfo.UseShellExecute = true;
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    myProcess.Start();
                    myProcess.WaitForExit();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

                //Turn off the hourglass cursor here.
                this.Cursor = Cursors.Default;
            


                    
        } //end of the start process function

        public Form1()
        {
            InitializeComponent();
            PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a = tabControl1.SelectedIndex;
            a++;
            tabControl1.SelectedIndex = a;

            if (tabControl1.SelectedTab.Name.Equals("tabPage8"))
                button1.Enabled = false;
            else
                button1.Enabled = true;
            button2.Enabled = true;

            if ((a== 3)&&(radioButton1.Checked == true))
            tabControl1.SelectTab(6);
           

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int a = tabControl1.SelectedIndex;
            a--;
            tabControl1.SelectedIndex = a;
            if (tabControl1.SelectedTab.Name.Equals("tabPage1"))
                button2.Enabled = false;
            else
                button2.Enabled = true;
            button1.Enabled = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = System.Environment.MachineName.ToString();
            sCompName = textBox1.Text;
            textBox2.Text = System.Environment.MachineName.ToString();
            sTrgtCompName = textBox2.Text;
            addcounters();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            textBox5.Text = folderBrowserDialog1.SelectedPath;

        }

      
        private void button7_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            myProcess.StartInfo = new ProcessStartInfo(@"logman.exe", cmd5); // for general
            
            if (radioButton2.Checked == true)
            {
              createcmd();
              myProcess.StartInfo = new ProcessStartInfo(@"logman.exe", cmd3); // for advanced
            }
            showcmd();
            starttheprocess();  // starting the perfmon logs - 

            // System.Diagnostics.Process.Start(@"logman", cmd3);
            //if (tabControl1.SelectedTab.Equals("tabPage7"));
            button7.Enabled= false;
            button1.Enabled = true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            myProcess.StartInfo = new ProcessStartInfo(@"logman.exe", cmd4);
            starttheprocess();
            //System.Diagnostics.Process.Start(@"logman", cmd4);
            Application.Exit();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            
           if (radioButton1.Checked == true) // for general logging
            {   makename();
                cmd5 = "create counter " + tn +  " -c \"\\LogicalDisk(*)\\*\" \"\\Network Interface(*)\\*\" \"\\Processor(*)\\*\" \"\\Memory\\*\" \"\\Paging File(*)\\*\" \"\\PhysicalDisk(*)\\*\" \"\\Server\\*\" \"\\Process(*)\\*\" \"\\System\\*\" \"\\Redirector\\*\"  -f bincirc -max 500 -si 00:00:15  -v mmddhhmm -ow";
               
            }
            button7.Enabled = true;

        }

        private void tabPage7_Enter(object sender, EventArgs e)
        {

            button1.Enabled = false;

            //if ((listBox1.Items.Count.Equals(0))&& ((radioButton2.Checked == true)))
            //MessageBox.Show("You have not selected any objects to add. Please go back and select some objects to monitor", "No objects selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            button7.Enabled = true;
        }

        private void tabPage6_Enter(object sender, EventArgs e)
        {
            button7.Enabled = true;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            showcmd(); // for populating the textbox with the commmand
        }

             

           
    }
}
